<?php
// +----------------------------------------------------------------------
// * Created by PhpStorm.
// +----------------------------------------------------------------------
// * Author: D H
// +----------------------------------------------------------------------
// * Date: 2017/9/4
// +----------------------------------------------------------------------
// * Time: 12:24
// +----------------------------------------------------------------------
namespace app\api\logic;
use think\Db;
use think\Session;
use tool\Common;
use think\Request;

class Merchant extends Base
{
    private $bucket = 'qc-product-image';

    //上传文件到阿里云对象存储
    public function upload()
    {
        $file = request()->file('file');
        $arr =  $file->getInfo();
        $path = $file->getpathName();
        $size = $file->getSize();
        //判断上传图片的大小   最大5M
        if ($size > 5242880) {
            return Common::rm(-2,'最大图片只允许5M');
        }
        $ossClient = ossClient();
        $bucket = $this->bucket;
        $object = time() . $arr['name'];
        $result = uploadFile($ossClient,$bucket,$object,$path);
        if ($result == 1) {
            return Common::rm(1,'操作成功','http://qc-product-image.oss-cn-hangzhou.aliyuncs.com/'.$object);
        }
    }

    //商家上传产品                            待完善   产品参数   userID
    public function addProduct(){
        $data = $this->data;
        $check = $this->check(__FUNCTION__);
        if ($check['code'] != 1) {
            return $check;
        }
        $user = Session::get('userinfo');
        //添加数据进product表的数据
        $arr = [
            'sellerID' => $user['userID'],        // 用户登录以后获取
            'catalogID' => $data['catalogID'],
            'name' => $data['name'],
            'model' => $data['model'],
            'manufacturer' => $data['manufacturer'],
            'origin' => $data['origin'],
            'class' => $data['class'],
            'protocolPrice' => $data['protocolPrice'],
            'marketsPrice' => $data['marketsPrice'],
            'addtime' => THINK_START_TIME
        ];
        //第一步判断品牌是否存在 存在返回ID 不存在添加返回ID
        $exist = Db::name('brand')->where('name',$data['brandName'])->find();
        if ($exist){
            $arr['brandID'] = $exist['id'];
        }else{
            $brandID = Db::name('brand')->insertGetId(['name' => $data['brandName']]);
            $arr['brandID'] = $brandID;
        }
        //添加数据进产品表  判断该产品是否被同个公司重复添加
        $repetition = Db::name('product')->where('brandID',$arr['brandID'])
            ->where('catalogID',$arr['catalogID'])->where('name',$arr['name'])
            ->where('sellerID',3)
            ->find();
        if ($repetition){
            return Common::rm(-2,'该商品已经存在，请勿重复添加');
        }else{
            $productID = Db::name('product')->insertGetId($arr);
        }
        //添加数据进内容表
        $details = json_encode($data['details']);
        $titleImage = json_encode($data['titleImage']);
        $parameter = json_encode($data['parameterList']);
        $content = [
          'productID' => $productID,              //产品ID 添加产品时获取
          'details' => $details,
          'titleImage' => $titleImage,
          'parameter' => $parameter
        ];
        $res = Db::name('product_content')->insert($content);
        if ($res > 0 ){
            return Common::rm(1,'添加商品成功');
        }else{
            return Common::rm(-2,'添加商品失败');
        }
    }

    //根据分类得到产品参数
    public function geParameterByCatID(){
      $data = [
          'catID' => 1
      ];
      $list = Db::name('cat_parameter')->where('catID',$data['catID'])->field('parameterName')->select();
      return Common::rm(1,'操作成功',['parameterList' => $list]);
    }

    //根据父类ID的到分类列表
    public function  getCatList(){
        $data = $this->data;
        $catList = Db::name('cat')->where('parentID',$data['parentID'])
            ->field(['id' => 'catID','name' => 'catName'])
            ->select();
        return Common::rm(1,'操作成功',['catList' => $catList]);
    }


}
